// pages/search/search.js
let that;
var http = require('../../common/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search_his: [],//历史搜索
    search_hto_list:[],//热门搜索的数据
    inputVal:''
  },
  aa(e){
    console.log(e);
    wx.navigateTo({
      url: '/pages/search/jieguo?tit=' + e.currentTarget.dataset.tit,
    })
  },
  //点击清空，清空数据和内存
  clear_search_his(){
    that.data.search_his=[];
    wx.setStorageSync('search_his', JSON.stringify(that.data.search_his));
    that.setData({
      search_his: that.data.search_his
    })
  },
  //点击热门搜索数据，跳转页面并传递参数
  search_hot_list(e){
    console.log(e.currentTarget.dataset.tit);
    wx.navigateTo({
      url: '/pages/search/jieguo?tit='+e.currentTarget.dataset.tit,
    })
  },
  //搜索框点击回车事件
  enter(e){
    console.log(e);
    var list = [];
    var arr =e.detail.value;
    list.push(arr);
   
    for(var i=0;i<list.length;i++){
      if (that.data.search_his.indexOf(list[i])==-1){
        that.data.search_his.push(list[i]);
      }
    }
    wx.setStorageSync('search_his', that.data.search_his);
    wx.navigateTo({
      url: '/pages/search/jieguo?tit=' + e.detail.value,
    })
    that.setData({
      search_his:that.data.search_his,
      inputVal:''//让input值回车事件添加后，在清空value值
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.search_hot();
    that.setData({
      search_his: wx.getStorageSync('search_his') ||[]
    })
  },
  async search_hot(){
    var res = await http.search_hot();
    this.setData({
      search_hto_list: res.data
    })
    // wx.request({
    //   url: 'https://api.xintujing.cn/v1/product/course/hot',
    //   success:res=>{
    //     console.log(res);
    //     this.setData({
    //       search_hto_list:res.data
    //     })
    //   },
    //   fail:msg=>{
    //     console.log(msg);
    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})